import time
from tkinter.tix import Select

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait


class Base():
    def __init__(self,driver):
        self.driver = driver

    def open_url(self,url):
        self.driver.get(url)

    def find_element(self,loc):
        try:
            WebDriverWait(self.driver,20,0.5).until(lambda driver:driver.find_element(*loc))
            return self.driver.find_element(*loc)
        except Exception as e:
            print(e)


    def click(self,loc):
        self.find_element(loc).click()


    def send_keys(self,loc,info):
        self.find_element(loc).send_keys(info)

    def text(self,loc):
        return  self.find_element(loc).text

    #进入框架
    def frame(self,loc):
        self.driver.switch_to.frame(self.driver.find_element(loc))

    #退出框架
    def tui_frame(self):
        self.driver.switch_to.default_content()
    #截图
    def time_jietu(self):
        et = time.strftime('%Y%m%d%H%M%S',time.localtime())
        self.driver.save_screenshot('..Png'+'Img'+et+'png')

    #窗口切换
    def new_window(self,l):
        self.driver.switch_to.window(self.driver.window_handles[l])

    #弹框处理
    def alert(self):
        self.driver.switch_to.alert.accept()

    #键盘事件
    def keys_enter(self,loc):
        self.find_element(loc).send_keys(Keys.ENTER)

    #鼠标事件
    def a_click(self,loc):
        ActionChains(self.driver).click(self.find_element(loc)).perform()
    # 浏览器事件
    def close_driver(self):
        self.driver.close()

    #下拉框
    def select(self,loc,x):
        select = Select(self.find_element(loc))
        select.select_by_value(x)

